package net.wyf.codetest.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import net.wyf.codetest.R;

/**
 * 复合式自定义View
 */

public class TopBarView extends RelativeLayout {
    public TopBarView(Context context) {
        this(context, null);
    }

    public TopBarView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TopBarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private RelativeLayout leftRelativeLayout, rightRelativeLayout, backgroundRelativeLayout;
    private ImageView leftImageView, rightImageView;
    private TextView titleTextView;

    private String mtitleContent;
    private float mtitleSize;
    private int mtitleColor, mbackground;
    private Drawable mleftBack = null, mrightBack = null;

    private void init(Context context, AttributeSet attrs) {
        View view = LayoutInflater.from(context).inflate(R.layout.top_bar, null, false);
        leftRelativeLayout = view.findViewById(R.id.left_button);
        rightRelativeLayout = view.findViewById(R.id.right_button);
        backgroundRelativeLayout = view.findViewById(R.id.back);
        leftImageView = view.findViewById(R.id.left);
        rightImageView = view.findViewById(R.id.right);
        titleTextView = view.findViewById(R.id.title);


        //前面在attr中定义过view的各种属性及其数据类型，我们这里就可以通过以下方法获取到xml中的对应属性值
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TopBar);
        mtitleContent = ta.getString(R.styleable.TopBar_myTitleTextContent);
        mtitleSize = ta.getDimension(R.styleable.TopBar_myTitleTextSize, 15);
        mtitleColor = ta.getColor(R.styleable.TopBar_myTitleTextColor, context.getResources().getColor(R.color.colorPrimary));
        mleftBack = ta.getDrawable(R.styleable.TopBar_leftBackground);
        mrightBack = ta.getDrawable(R.styleable.TopBar_rightBackground);
        mbackground = ta.getColor(R.styleable.TopBar_myBackground, Color.BLACK);
        ta.recycle();

        show();
        bindEvents();
        addView(view);
    }

    private void bindEvents() {
        leftRelativeLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    mListener.leftClick();
                }
            }
        });

        rightRelativeLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    mListener.rightClick();
                }
            }
        });
    }

    private OnTopBarClickListener mListener;

    /**
     * 定义左右button点击的接口
     */
    public interface OnTopBarClickListener {
        //左边的点击事件
        void leftClick();

        //右边按钮的点击事件
        void rightClick();
    }

    /**
     * 暴露一个接口供调用者来注册接口回调
     * 通过接口来获得回调这对接口方法的实现
     */
    public void setOnTopbarClickListener(OnTopBarClickListener mListener) {
        this.mListener = mListener;
    }

    private void show() {
        titleTextView.setText(mtitleContent);
        titleTextView.setTextColor(mtitleColor);
        titleTextView.setTextSize(mtitleSize);
        setImageBackGround(leftImageView, mleftBack);
        setImageBackGround(rightImageView, mrightBack);
        backgroundRelativeLayout.setBackgroundColor(mbackground);
    }

    private void setImageBackGround(ImageView imageview, Drawable drawable) {
        if (drawable != null) {
            imageview.setImageDrawable(drawable);
        } else {
            imageview.setBackgroundResource(R.mipmap.ic_launcher);
        }
    }
}
